package com.yefeng.usercenter.handler;

import com.yefeng.usercenter.domain.response.BaseResponse;
import com.yefeng.usercenter.exception.BusinessException;
import com.yefeng.usercenter.exception.enums.ErrorCode;
import com.yefeng.usercenter.util.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
* @author Shier
* 全局异常处理器
*/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
/**
* 捕获BusinessException异常
*
* @param e
* @return
*/
@ExceptionHandler(BusinessException.class)
public BaseResponse businessExceptionHandler(BusinessException e) {
// 日志集中处理
log.error("businessException:" + e.getMessage(), e);
return ResultUtils.error(e.getCode(), e.getMessage(),
e.getDescription());
}
/**
* 捕获RuntimeException异常
*
* @param e 异常
* @return
*/
@ExceptionHandler(RuntimeException.class)
public BaseResponse runtimeExceptionHandler(BusinessException e) {
// 日志集中处理
log.error("runtimeException:", e);
return ResultUtils.error(ErrorCode.SYSTEM_ERROR, e.getMessage(), "");
}
}
